package com.meima.controller;

import com.meima.entity.Order;
import com.meima.entity.User;
import com.meima.service.OrderService;
import com.meima.user.client.DemoClient;
import com.meima.user.client.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderService orderService;

    // @Autowired
    // RestTemplate restTemplate;

    @Autowired
    UserClient userClient;

    @Autowired
    DemoClient demoClient;

    @GetMapping("/{id}")
    public Order findById(@PathVariable Long id, @RequestHeader("Truth") String truth){
        // 根据订单id，查询订单信息
        Order order = orderService.findById(id);
        // 取得订单信息中的userid
        Long userId = order.getUserId();
        // 就是通过http请求，请求User里面findById方法
        // User user = restTemplate.getForObject("http://userservice/user/" + userId, User.class);

        // 利用feign替代了restTemplate
        User user = userClient.findById(userId);
        // 将用户对象，绑定给订单对象
        order.setUser(user);
        System.out.println("--------------truth:"+truth);
        return order;
    }

    @RequestMapping("demo")
    public String demo(){
        return demoClient.hello();
    }
}
